//
//  LocationSelectView.m
//  WinGenProject
//
//  Created by MacPro on 15/12/6.
//  Copyright © 2015年 Winthegen. All rights reserved.
//

#import "LocationSelectView.h"
#import "RATreeView.h"

#define kHomeTableGeight 40.0f

@interface LocationSelectView ()<RATreeViewDelegate, RATreeViewDataSource>
{
    RATreeView *_locationTableView;;
    
}

@property (strong, nonatomic) RADataObject *selectedData;

@end

@implementation LocationSelectView

@synthesize locationArray = _locationArray;

- (instancetype)initLocationSelectView
{
    self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        
        UIView *clearView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        clearView.backgroundColor = [UIColor clearColor];
        [self addSubview:clearView];
        
        UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*2/5.0f, 64.0f, SCREEN_WIDTH*3/5.0f, SCREEN_HEIGHT - 64.0f - 44.0f)];
        backView.backgroundColor = HexRGB(0xe2e2e2);
        [clearView addSubview:backView];
        
        //弹框消失手势
        UITapGestureRecognizer *doHomeTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doLocationViewHidden)];
        [clearView addGestureRecognizer:doHomeTap];
        
        UIView *containerView = [[UIView alloc] initWithFrame:backView.frame];
        containerView.backgroundColor = [UIColor clearColor];
        
        UILabel *noticeLabel= [[UILabel alloc] initWithFrame:CGRectMake(0, 0, containerView.width, 40.0f)];
        noticeLabel.backgroundColor = HexRGB(0xeaeaea);
        noticeLabel.font = Default_Font_14;
        noticeLabel.textColor = HexRGB(0x7c7c7c);
        noticeLabel.text = @"  选择区域";
        [containerView addSubview:noticeLabel];
        
        _locationTableView = [[RATreeView alloc] initWithFrame:CGRectMake(0, 20.0f, containerView.width, containerView.height)];
        _locationTableView.delegate = self;
        _locationTableView.dataSource = self;
        _locationTableView.separatorStyle = RATreeViewCellSeparatorStyleSingleLine;
        [_locationTableView setBackgroundColor:[UIColor clearColor]];
        [containerView addSubview:_locationTableView];
        
        UIView *footView = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, _locationTableView.width, 80)];
        footView.backgroundColor = [UIColor clearColor];
        
        UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        confirmBtn.frame = CGRectMake(footView.right - 100.0f, 10, 60.0f, 30);
        confirmBtn.backgroundColor = DefaultColor_NavigationBar;
        [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        confirmBtn.titleLabel.font = Default_Font_14;
        [confirmBtn addTarget:self action:@selector(clickSubmitBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:confirmBtn];
        
        _locationTableView.treeFooterView = footView;
        _locationTableView.treeHeaderView = [[UIView alloc] initWithFrame:CGRectZero];

        [self addSubview:containerView];
        
    }

    return self;
}

#pragma mark TreeView Delegate methods
- (CGFloat)treeView:(RATreeView *)treeView heightForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    return kHomeTableGeight;
}

- (NSInteger)treeView:(RATreeView *)treeView indentationLevelForRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    return 2 * treeNodeInfo.treeDepthLevel;
}

- (BOOL)treeView:(RATreeView *)treeView shouldExpandItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    return YES;
}

- (BOOL)treeView:(RATreeView *)treeView shouldItemBeExpandedAfterDataReload:(id)item treeDepthLevel:(NSInteger)treeDepthLevel
{
    return NO;
}

- (void)treeView:(RATreeView *)treeView willDisplayCell:(UITableViewCell *)cell forItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    cell.backgroundColor = [UIColor clearColor];
}

#pragma mark TreeView Data Source

- (UITableViewCell *)treeView:(RATreeView *)treeView cellForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    
    static NSString *identify = @"treeCell";
    
    UITableViewCell *cell = [treeView dequeueReusableCellWithIdentifier:identify];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        cell.backgroundColor = [UIColor clearColor];
        cell.contentView.backgroundColor = [UIColor clearColor];
        
        UIButton *arrowBtn = [[UIButton alloc] initWithFrame:CGRectMake(_locationTableView.width - 35.0f, 12.5f, 15.0f, 15.0f)];
        NSString *path = [[NSBundle mainBundle] pathForResource:@"Guide_rightArrow" ofType:@"png"];
        [arrowBtn setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
        
        NSString *pathDown = [[NSBundle mainBundle] pathForResource:@"Guide_downArrow" ofType:@"png"];
        [arrowBtn setImage:[UIImage imageWithContentsOfFile:pathDown] forState:UIControlStateSelected];
        arrowBtn.tag = 999;
        arrowBtn.hidden = YES;
        arrowBtn.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:arrowBtn];
    }
    
    UIButton *arrowBtn = (UIButton *)[cell viewWithTag:999];
    if ([((RADataObject *)item).children count] == 0) {
        
        arrowBtn.hidden = YES;

    }else {
        arrowBtn.hidden = NO;;
    }
    
    NSString *locationStr = ((RADataObject *)item).name;
    
    cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
    cell.textLabel.textColor = HexRGB(0x7c7c7c);;
    cell.textLabel.text = locationStr;
    
    
    return cell;
}

- (NSInteger)treeView:(RATreeView *)treeView numberOfChildrenOfItem:(id)item
{
    if (item == nil) {
        return [_locationArray count];
    }
    RADataObject *data = item;
    return [data.children count];
}

- (id)treeView:(RATreeView *)treeView child:(NSInteger)index ofItem:(id)item
{
    RADataObject *data = item;
    if (item == nil) {
        return [_locationArray objectAtIndex:index];
    }
    return [data.children objectAtIndex:index];
}

- (void)treeView:(RATreeView *)treeView didSelectRowForItem:(id)item treeNodeInfo:(RATreeNodeInfo *)treeNodeInfo
{
    
    UITableViewCell *cell = (UITableViewCell *)[treeView cellForItem:item];
    
    RADataObject *data = (RADataObject *)item;
    
    if ([data.children count] != 0) {
        UIButton *arrowBtn = (UIButton *)[cell viewWithTag:999];
        arrowBtn.selected = !arrowBtn.selected;
        [treeView deselectRowForItem:item animated:YES];
    }
    
    self.selectedData = data;
}

//点击确定触发
- (void)clickSubmitBtnAction:(UIButton *)sender
{

    if (self.SelectRowAtIndexPathBlock) {
        self.SelectRowAtIndexPathBlock(self.selectedData);
    }
    
    //取消弹出框
    [self doLocationViewHidden];
}

//隐藏本黑色的弹出view
- (void)doLocationViewHidden
{
    [UIView animateWithDuration:0.5 animations:^{
        
//        [self removeFromSuperview];
        
    }];
    
}

//更新表格数据
- (void)setLocationArray:(NSArray *)locationArray
{
    _locationArray = locationArray;
    if (_locationTableView) {

        [_locationTableView reloadData];
        
    }
}

@end
